package capricornus.actor.res
{
	import flash.display.BitmapData;

	public class ActionImage
	{
		internal var parent:ActionImage;
		internal var isClone:Boolean = false;
		internal var count:int = 0;
		
		public var angles:int = 1;   //表示有几方向的图
		public var frames:int = 0;   //表示每个方向的图片动作帧数
		
		public var arrOffset:Array = new Array(); //每个图片的偏移值
		
		public var arrImages:Array = new Array(); //图片
		
		public function ActionImage()
		{
		}
		
		public function destroy():void
		{
			if( isClone )
			{
				count--;
				if( count < 1 )
				{
					parent.destroy();
				}
				parent = null;
			}
			else
			{
				/*arrOffset.length = 0;
				while( arrImages.length > 0 )
				{
					BitmapData(arrImages.pop()).dispose();
				}*/
			}
		}
		
		public function clone():ActionImage
		{
			var ai:ActionImage = new ActionImage();
			
			ai.isClone = true;
			ai.parent = this;
			ai.angles = angles;
			ai.frames = frames;
			ai.arrOffset = arrOffset;
			ai.arrImages = arrImages;
			
			count++;
			
			return ai;
		}
		
		public function getOffset( direct:int, frame:int ):Array
		{
			if( direct < arrOffset.length && arrOffset[direct].length > frame )
			{
				return arrOffset[direct][frame];
			}
			return arrOffset[0][0];
		}
		
		public function getImage( direct:int, frame:int ):BitmapData
		{
			var n:int = direct * frames + frame;
			if( n < arrImages.length )
			{
				return arrImages[n];
			}
			return arrImages[0];
		}
		
		//获取正对角方向值
		public static function getOppositeDirect( value:int ):int
		{
			return (value + 4) % 8;
		}
		
		private static var _dai:ActionImage;
		public static function defaultActionImage():ActionImage
		{
			if( _dai==null )
			{
				_dai = new ActionImage();
				_dai.frames = 0;
				var bd:BitmapData = new DefaultActionImagePanel();
				_dai.arrOffset.push( [[int(bd.width/2), int(bd.height/2)]] );
				_dai.arrImages.push( bd );
			}
			return _dai;
		}
	}
}